<template>
  <div class="p-4">
    <div class="flex justify-between items-center mb-4">
      <h1 class="text-2xl font-bold text-slate-800 ">RSS 源管理</h1>
      <el-button type="primary" @click="goAdd">新增</el-button>
    </div>

    <el-card class="mb-4" shadow="never">
      <el-form :inline="true" :model="query">
        <el-form-item label="名称">
          <el-input v-model="query.name" clearable placeholder="输入名称" style="width: 220px" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="query.status" placeholder="全部" clearable style="width: 140px">
            <el-option :value="1" label="启用" />
            <el-option :value="0" label="禁用" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="reload" :loading="loading">
            <el-icon class="mr-1"><Search /></el-icon>搜索
          </el-button>
          <el-button @click="reset">
            <el-icon class="mr-1"><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="never">
      <template #header>
        <div class="flex justify-between items-center">
          <div class="text-lg font-medium">RSS 源列表</div>
        </div>
      </template>
      <el-table :data="rows" v-loading="loading" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="名称" min-width="180" show-overflow-tooltip />
        <el-table-column prop="url" label="URL" min-width="240" show-overflow-tooltip />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">{{ scope.row.status === 1 ? '启用' : '禁用' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="fetchTime" label="抓取时间" width="120" />
        <el-table-column prop="rssVersion" label="RSS版本" width="110" />
        <el-table-column prop="rssSourceType" label="来源类型" width="110" />
        <el-table-column label="操作" width="240" fixed="right">
          <template #default="scope">
            <el-button size="small" text @click="goEdit(scope.row.id)">编辑</el-button>
            <el-button size="small" text @click="toggleStatus(scope.row)">{{ scope.row.status === 1 ? '禁用' : '启用' }}</el-button>
            <el-button size="small" text type="danger" @click="remove(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <el-pagination
          v-model:current-page="pageNum"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="reload"
          @current-change="reload"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Search, Refresh } from '@element-plus/icons-vue'
import { rssFeedAPI } from '@/services/api'

definePageMeta({ layout: 'admin' })

const loading = ref(false)
const rows = ref([])
const total = ref(0)
const pageNum = ref(1)
const pageSize = ref(10)
const query = ref({ name: '', status: undefined })

const reload = async () => {
  loading.value = true
  try {
    const data = await rssFeedAPI.getPage({ pageNum: pageNum.value, pageSize: pageSize.value, name: query.value.name, status: query.value.status })
    rows.value = data.list || []
    total.value = data.total || 0
  } finally {
    loading.value = false
  }
}

const reset = () => {
  query.value = { name: '', status: undefined }
  pageNum.value = 1
  reload()
}

const goAdd = () => navigateTo('/admin/fetch/rss/add')
const goEdit = (id) => navigateTo(`/admin/fetch/rss/edit/${id}`)

const toggleStatus = async (row) => {
  const to = row.status === 1 ? 0 : 1
  await rssFeedAPI.updateStatus(row.id, to)
  reload()
}

const remove = async (id) => {
  await rssFeedAPI.remove(id)
  reload()
}

onMounted(reload)
</script>



